<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>餐饮管理系统登录</title>
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <meta http-equiv="Cache-Control" content="no-siteapp"/>

    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="./static/css/font.css">
    <link rel="stylesheet" href="./static/css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script src="./static/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="./static/js/xadmin.js"></script>


    <style>
        #canvas {
            float: right;
            display: inline-block;
            border: 1px solid #ccc;
            border-radius: 5px;
            cursor: pointer;
        }

    </style>
</head>
<body class="login-bg">

<div class="login layui-anim layui-anim-up">
    <div class="message">餐饮管理系统登录</div>
    <div id="darkbannerwrap"></div>

    <input id="workerLoginName" name="workerLoginName" placeholder="用户名" value="superadmin" type="text"
           class="layui-input">
    <hr class="hr15">
    <input id="workerPassword" name="workerPassword" value="123456" placeholder="密码"
           type="password" class="layui-input">
    <hr class="hr15">

    <div class="layui-row" id="showCaptcha" style="display: none">
        <div class="layui-col-xs7">
            <input type="text" id="captcha" class="layui-input" placeholder="请输入图形验证码">
        </div>
        <div class="layui-col-xs5">
            <div style="margin-left: 15px;">
                <canvas id="canvas" width="100" height="48"></canvas>
            </div>
        </div>
        <button class="layui-btn" id="captchaBtn"
                STYLE="margin-top:15px;  width:100%;height: 50px;font-size: large">
            确认图形验证码
        </button>
    </div>
    <button class="layui-btn" id="login"
            STYLE="width:100%;height: 50px;font-size: large">
        登录
    </button>
    <hr class="hr20">
    <button class="layui-btn layui-bg-red layui-btn-primary" style="width:100%;height: 50px;font-size: large"
            id="forgetPasswordBtn"
            onclick="showForgetPassword()">
        忘记密码
    </button>
    <br/>
    <br/>
    <div id="showForgetWorkerPhone" style="display: none">
        <label class="layui-form-label">
            输入手机号
        </label>
        <div class="layui-input-inline">
            <input type="text" maxlength="11" id="forgetWorkerPhone" name="forgetWorkerPhone" value="" class="layui-input">
        </div>
    </div>
    <br/>
    <label class="layui-form-label"></label>
    <button class="layui-btn layui-bg-orange" style="display: none" id="sendCodeBtn" onclick="sendCode()">
        发送验证码
    </button>
    <div class="layui-form-item">
        <label class="layui-form-label"></label>
        <input type="hidden" id="forgetWorkerTel" class="layui-input">
        <label class="layui-form-label"></label>
        <div id="forgetPassword"></div>
        <label class="layui-form-label"></label>
        <button class="layui-btn" id="submitCode"
                STYLE="display: none;width:100%;height: 50px;font-size: large">
            提交&重置
        </button>
    </div>
    <label class="layui-form-label"></label>
    <button class="layui-btn" id="resetPassword" lay-filter="resetPassword" lay-submit="" STYLE="display: none">确定重置
    </button>
</div>

<script>
    var IntervalTime;//timer变量，控制时间
    var count = 60;//间隔函数，1秒执行
    var surcount;//当前剩余秒数
    var relcode = ""//下发的验证码
    var loginerrortimes = 0;//错误登录次数
    var show_num;//图形验证码参数

    function sendCode() {
        if (checkforgetWorkerPhone()) {
            var phone = $("#forgetWorkerPhone").val()
            var loading = layer.load(2, {
                shade: [0.8, '#fff'],
                content:'验证码发送中...',
                success: function (layerContentStyle) { // 设置loading样式
                    layerContentStyle.find('.layui-layer-content').css({
                        'padding-left': '35px',
                        'color':'#FF3366',
                        'text-align': 'left',
                        'width': '175px',
                        'line-height':'30px'
                    });
                }
            });
            $.ajax({
                async: false,
                type: "post",
                url: "${pageContext.request.contextPath}/login?m=sendCodeByForgetWorkerPhone",
                data: {
                    forgetWorkerPhone: phone
                },
                success: function (data) {
                    if (data.code === 200) {
                        layer.close(loading);
                        relcode = data.data;
                        layer.msg(data.msg, {icon: 1});
                        surcount = count;
                        $("#sendCodeBtn").addClass("layui-bg-gray layui-btn-disabled").attr("disabled", true);
                        $("#sendCodeBtn").text(surcount + "秒后可重新发送");
                        $("#forgetWorkerTel").val($("#forgetWorkerPhone").val());
                        IntervalTime = window.setInterval(SetReTimer, 1000)
                    }else {
                        layer.close(loading);
                        layer.msg(data.msg, {icon: 5})
                    }
                }
            });
        }

    }

    function SetReTimer() {
        if (surcount == 0) {
            window.clearInterval(IntervalTime);//停止计时器
            $("#sendCodeBtn").removeClass("layui-bg-gray layui-btn-disabled").attr("disabled", false);
            $("#sendCodeBtn").text("重新发送验证码");
        } else {
            surcount--;
            $("#sendCodeBtn").text(surcount + "秒后可重新发送");
        }
    }

    //忘记密码
    var isForget = false;

    function showForgetPassword() {
        if (!isForget) {
            $("#forgetPassword").append(
                '<div class="layui-form-item" disabled="disabled">\n' +
                '<label class="layui-form-label">\n' +
                '输入验证码\n' +
                '</label>\n' +
                '<div class="layui-input-inline">\n' +
                '<input type="number" id="Code" name="Code" value=""\n' +
                'autocomplete="off" class="layui-input">\n' +
                '</div>\n' +
                '</div>\n' +
                '<div class="layui-form-item" disabled="disabled">\n' +
                '<label class="layui-form-label">\n' +
                '重置密码\n' +
                '</label>\n' +
                '<div class="layui-input-inline">\n' +
                '<input type="password" id="resetPassword" name="resetPassword" value=""\n' +
                'autocomplete="off" class="layui-input">\n' +
                '</div>\n' +
                '</div>\n' +
                '<div class="layui-form-item">\n' +
                '<label class="layui-form-label">\n' +
                '确认密码\n' +
                '</label>\n' +
                '<div class="layui-input-inline" style="height: 30px">\n' +
                '<input type="password" id="resetPassword2" name="resetPassword2" value=""\n' +
                'autocomplete="off" class="layui-input">\n' +
                '</div>\n' +
                '</div>')
            $("#forgetPasswordBtn").addClass("layui-bg-blue");
            $("#forgetPasswordBtn").text("想起密码了");
            $("#showForgetWorkerPhone").show();
            $("#forgetWorkerPhone").val("");
            $("#sendCodeBtn").show();
            $("#submitCode").show();
            isForget = true;

        } else {
            $("#forgetPassword").empty();
            $("#sendCodeBtn").text("发送验证码");
            $("#forgetPasswordBtn").removeClass("layui-bg-blue");
            $("#forgetPasswordBtn").text("忘记密码");
            $("#submitCode").hide();
            $("#sendCodeBtn").hide();
            $("#showForgetWorkerPhone").hide();
            isForget = false;
        }
    };

    function CreateCaptcha() {
        $("#login").hide();
        show_num = [];
        draw(show_num);

    }

    $("#canvas").click(function () {
        draw(show_num);
    })
    $("#captchaBtn").click(function () {
        var val = $("#captcha").val().toLowerCase();
        var num = show_num.join("");
        if (val == '') {
            layer.msg('请输入验证码', {icon: 5});
        } else if (val == num) {
            $("#captcha").val('');
            draw(show_num);
            loginDo();
        } else {
            layer.msg('验证码错误！请重新输入！', {icon: 5});
            $("#captcha").val('');
            draw(show_num);
        }
    })


    function draw(show_num) {
        var canvas_width = $('#canvas').width();
        var canvas_height = $('#canvas').height();
        var canvas = document.getElementById("canvas");//获取到canvas的对象，演员
        var context = canvas.getContext("2d");//获取到canvas画图的环境，演员表演的舞台
        canvas.width = canvas_width;
        canvas.height = canvas_height;
        var sCode = "A,B,C,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,W,X,Y,Z,1,2,3,4,5,6,7,8,9,0";
        var aCode = sCode.split(",");
        var aLength = aCode.length;//获取到数组的长度

        for (var i = 0; i <= 3; i++) {
            var j = Math.floor(Math.random() * aLength);//获取到随机的索引值
            var deg = Math.random() * 30 * Math.PI / 180;//产生0~30之间的随机弧度
            var txt = aCode[j];//得到随机的一个内容
            show_num[i] = txt.toLowerCase();
            var x = 10 + i * 20;//文字在canvas上的x坐标
            var y = 20 + Math.random() * 8;//文字在canvas上的y坐标
            context.font = "bold 23px 微软雅黑";

            context.translate(x, y);
            context.rotate(deg);

            context.fillStyle = randomColor();
            context.fillText(txt, 0, 0);

            context.rotate(-deg);
            context.translate(-x, -y);
        }
        for (var i = 0; i <= 5; i++) { //验证码上显示线条
            context.strokeStyle = randomColor();
            context.beginPath();
            context.moveTo(Math.random() * canvas_width, Math.random() * canvas_height);
            context.lineTo(Math.random() * canvas_width, Math.random() * canvas_height);
            context.stroke();
        }
        for (var i = 0; i <= 30; i++) { //验证码上显示小点
            context.strokeStyle = randomColor();
            context.beginPath();
            var x = Math.random() * canvas_width;
            var y = Math.random() * canvas_height;
            context.moveTo(x, y);
            context.lineTo(x + 1, y + 1);
            context.stroke();
        }
    }

    function randomColor() {//得到随机的颜色值
        var r = Math.floor(Math.random() * 256);
        var g = Math.floor(Math.random() * 256);
        var b = Math.floor(Math.random() * 256);
        return "rgb(" + r + "," + g + "," + b + ")";
    }

    $("#submitCode").click(function () {
        if (checkCode()) {
            //发异步，把数据提交给java
            if (checkResetPassword()) {
                $.ajax({
                    type: "post",
                    url: "${pageContext.request.contextPath}/login?m=resetPasswordByForgetWorkerPhone",
                    data: {
                        forgetWorkerPhone: $("#forgetWorkerTel").val(),
                        resetPassword: $("#resetPassword").val()
                    },
                    success: function (data) {
                        layer.msg(data.msg, function () {
                            if (data.code === 200) {
                                window.parent.location.reload();
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                            }
                        });
                    }
                });
            }
        }
    })

    function checkAccount() {
        var workerLoginName = $("#workerLoginName").val()//用户名
        var workerPassword = $("#workerPassword").val()//密码
        if (workerLoginName!=""&&workerPassword!=""){
            return true
        }else {
            layer.msg("请输入用户名和密码",{icon:5})
        }
    }
    function checkCode() {
        var res = /^\d{6}$/;
        var code = $("#Code").val();//短信验证码
        if (res.test(code)) {
            if (relcode == code) {
                return true;
            } else {
                layer.msg('验证码错误', {icon: 5});
                return false;
            }
        } else {
            layer.msg('请输入正确的验证码', {icon: 5});
            return false;
        }
    }

    function checkResetPassword() {
        var resetPassword = $("#resetPassword").val();
        var resetPassword2 = $("#resetPassword2").val();
        if (checkPwd(resetPassword)&&checkPwd(resetPassword2)) {
            if (resetPassword == resetPassword2) {
                return true;
            } else {
                layer.msg('两次密码不一致', {icon: 5})
                return false;
            }
        }
        {
            layer.msg('密码必须6-12位', {icon: 5})
            return false;
        }
    }

    function checkPwd(str) {
        var reg = /^(.+){6,12}/
        if (reg.test(str)) {
            return true;
        } else {
            return false;
        }
    }

    function checkforgetWorkerPhone() {
        var reg = /^1[0-9]{10}$/
        if (reg.test($("#forgetWorkerPhone").val())) {
            return true
        } else {
            layer.msg('请输入正确的手机号码', {icon: 5});
            return false
        }
    }

    $("#login").click(function () {
        if (checkAccount()){
            var workerLoginName = $("#workerLoginName").val()//用户名
            var workerPassword = $("#workerPassword").val()//密码
            $.ajax({
                type: "post",
                url: "${pageContext.request.contextPath}/login?m=getLoginerrorTimes",
                data: {
                    workerLoginName: workerLoginName,
                    workerPassword: workerPassword
                },
                success: function (data) {
                    if (data.code === 200) {
                        loginerrortimes = data.data;
                        if (loginerrortimes < 3) {
                            loginDo()
                        } else {
                            $("#showCaptcha").show();
                            CreateCaptcha()
                        }
                    } else {
                        layer.msg(data.msg, {icon: 5});
                    }
                }
            });
        }

    });

    function loginDo() {
        var workerLoginName = $("#workerLoginName").val()//用户名
        var workerPassword = $("#workerPassword").val()//密码
        $.ajax({
            type: "post",
            url: "${pageContext.request.contextPath}/login?m=loginDo",
            data: {
                workerLoginName: workerLoginName,
                workerPassword: workerPassword
            },
            success: function (data) {
                layer.msg(data.msg, {icon: data.code === 200 ? 6 : 5}, function () {
                    if (data.code === 200) {
                        location.href = '${pageContext.request.contextPath}/index?m=indexPage';
                    } else {
                        showBtn();
                    }
                });
            }
        });
    }

    function showBtn() {
        $("#login").show();
        $("#showCaptcha").hide();
    }

    var bgimgs = [];
    for (let i = 0; i < 30; i++) {
        bgimgs[i] = "/static/images/bg" + (i + 1) + ".png";
    }
    var randomBgIndex = Math.round(Math.random() * bgimgs.length);
    //输出随机的背景图
    document.write('<style>body{background:url(' + bgimgs[randomBgIndex + 1] + ') no-repeat center}</style>');

</script>
<!-- 底部结束 -->
<script type="text/javascript" src="./static/js/canvas-nest.js"></script>
</body>
</html>